import AdmZip from 'adm-zip';
import XmlJs from 'xml2js';
import path from 'path';
import fs from 'fs';

export function unzip(bookPath, unzipPath) {
  const zip = new AdmZip(bookPath);
  zip.extractAllTo(unzipPath, true);
}

export function parseRootFile(unzipPath) {
  const containerFilePath = path.resolve(unzipPath, 'META-INF/container.xml');
  const containerXml = fs.readFileSync(containerFilePath, 'utf-8');
  return XmlJs.parseStringPromise(containerXml, {}).then((data) => {
    return data.catalog.book;
  });
}
